package cn.codergege.demo.config;

import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;

import javax.sql.DataSource;


@Configuration
@PropertySource("classpath:/application.properties")
public class DataSourceConfig {

    @Autowired
    private Environment env;

    @Bean
    public DataSource dataSource() {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName(env.getProperty("mysql.driver"));
        ds.setUrl(env.getProperty("mysql.url"));
        ds.setUsername(env.getProperty("mysql.username"));
        ds.setPassword(env.getProperty("mysql.password"));
        ds.setInitialSize(Integer.parseInt(env.getProperty("initialSize")));
        ds.setMaxIdle(Integer.parseInt(env.getProperty("maxIdle")));
        ds.setMaxTotal(Integer.parseInt(env.getProperty("maxTotal")));

        return ds;
    }

    @Bean
    public DataSource embeddedDataSource() {
        return new EmbeddedDatabaseBuilder()
                .setType(EmbeddedDatabaseType.H2)
                .addScript("classpath:sql/h2-schema.sql")
                .addScript("classpath:sql/h2-test-data.sql")
                .build();
    }
}
			